package com.hand.wfive.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.hand.wfive.base.BaseFragment;

import java.util.ArrayList;

/**
 * @author Doris
 * @date 2021/4/2
 **/
public class FragmentAdapter extends FragmentPagerAdapter {

    private ArrayList<BaseFragment> fragments;
    private ArrayList<String> titles;

    public FragmentAdapter(@NonNull FragmentManager fm,
                           ArrayList<BaseFragment> fragments) {
        super(fm);
        this.fragments = fragments;
        if (this.fragments == null) {
            this.fragments = new ArrayList<>();
        }
    }

    public FragmentAdapter(@NonNull FragmentManager fm,
                           ArrayList<BaseFragment> fragments,
                           ArrayList<String> titles) {
        super(fm);
        this.fragments = fragments;
        if (this.fragments == null) {
            this.fragments = new ArrayList<>();
        }
        this.titles = titles;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        if (titles != null && titles.size() == fragments.size()){
            return titles.get(position);
        }
        return super.getPageTitle(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

}
